//
//  DisplayViewTree.m
//  YuiKe
//
//  Created by yuike6 yuike6 on 11-9-7.
//  Copyright 2011年 yuike. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DisplayViewTree.h"

@implementation DisplayViewTree
// Recursively travel down the view tree, increasing the indentation level for children
+ (void) dumpView: (UIView *) aView atIndent: (int) indent into:(NSMutableString *) outstring deep:(int)deep
{
    if (indent > deep) {
        return;//???
    }
	for (int i = 0; i < indent; i++) [outstring appendString:@"--"];
    CGRect frame = aView.frame;
    
    NSString* temp = @"";
    if ([aView isKindOfClass:[UIScrollView class]]) {
        UIScrollView* scroll = (UIScrollView*) aView;
        temp = [NSString stringWithFormat:@"Size(%.0f,%.0f)Offset(%.0f,%.0f)", 
                                            scroll.contentSize.width, scroll.contentSize.height,
                                            scroll.contentOffset.x, scroll.contentOffset.y];
    }
    
    if (aView.tag != 0) {
        [outstring appendFormat:@"[%2d] %@ -- %d(%.0f, %.0f, %.0f, %.0f)%@\n", indent, [[aView class] description], aView.tag,
         frame.origin.x, frame.origin.y, frame.size.width, frame.size.height, temp];
    } else {
        [outstring appendFormat:@"[%2d] %@ -- (%.0f, %.0f, %.0f, %.0f)%@\n", indent, [[aView class] description],
         frame.origin.x, frame.origin.y, frame.size.width, frame.size.height, temp];
    }

	for (UIView *view in [aView subviews]) [self dumpView:view atIndent:indent + 1 into:outstring deep:deep];
}

+ (BOOL) xView:(UIView*)view include:(UIView*)xView {
    if (view == xView) {
        return true;
    }
    for (UIView* temp in view.subviews) {
        if ([self xView:temp include:xView]) {
            return true;
        }
    }
    return false;
}
+ (void) dumpView: (UIView *) aView atIndent: (int) indent into:(NSMutableString *) outstring deep:(int)deep xInclude:(UIView*)xView
{
    if (indent > deep) {
        return;//???
    }
	for (int i = 0; i < indent; i++) [outstring appendString:@"--"];
    CGRect frame = aView.frame;
    
    NSString* temp = @"";
    if ([aView isKindOfClass:[UIScrollView class]]) {
        UIScrollView* scroll = (UIScrollView*) aView;
        temp = [NSString stringWithFormat:@"Size(%.0f,%.0f)Offset(%.0f,%.0f)", 
                scroll.contentSize.width, scroll.contentSize.height,
                scroll.contentOffset.x, scroll.contentOffset.y];
    }
    
    if (aView.tag != 0) {
        [outstring appendFormat:@"[%2d] %@ -- %d(%.0f, %.0f, %.0f, %.0f)%@\n", indent, [[aView class] description], aView.tag,
         frame.origin.x, frame.origin.y, frame.size.width, frame.size.height, temp];
    }
    else {
        [outstring appendFormat:@"[%2d] %@ -- (%.0f, %.0f, %.0f, %.0f)%@\n", indent, [[aView class] description],
         frame.origin.x, frame.origin.y, frame.size.width, frame.size.height, temp];
    }
	for (UIView *view in [aView subviews]) {
        if ([self xView:aView include:xView] && aView != xView) {
            [self dumpView:view atIndent:indent + 1 into:outstring deep:deep xInclude:xView];
        }
    }
}

+(void) dumpViewPx:(UIView*)view into:(NSMutableString *)outstring {
    UIView* pview = view;
    while (pview.superview) {
        pview = pview.superview;
    }
    
    [self dumpView:pview atIndent:0 into:outstring deep:999 xInclude:view];
}


// Start the tree recursion at level 0 with the root view
+ (NSString *) dumpViews: (UIView *) aView deep:(int)deep
{
	NSMutableString *outstring = [[NSMutableString alloc] init];
	[self dumpView: aView atIndent:0 into:outstring deep:deep];
	return [outstring autorelease];
}
+ (NSString *) dumpViewsPx: (UIView *) aView
{
	NSMutableString *outstring = [[NSMutableString alloc] init];
	[self dumpViewPx:aView into:outstring];
	return [outstring autorelease];
}


// Show the tree
+ (void) displayViews: (UIView*) view
{
	CFShow([self dumpViews: view deep:10]);
}
+ (void) displayViews: (UIView*) view deep:(int)deep
{
    CFShow([self dumpViews: view deep:deep]);
}
+ (void) displayViewsPx: (UIView*) view
{
    CFShow([self dumpViewsPx: view]);
}

@end
